home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / test / timer.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  1.8 KB  |  66 lines  |  [TEXT/R*ch]

  1. (* test/timer.sml
  2.    PS 1995-03-20
  3. *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local 
  8.     fun fib n = if n<2 then 1 else fib(n-1) + fib(n-2);
  9.  
  10.     open Time Timer
  11.     val totalRealTime = totalRealTimer ()
  12.     val totalCPUTime  = totalCPUTimer ()
  13. in
  14.  
  15. val test1 = check(checkRealTimer totalRealTime <= checkRealTimer totalRealTime
  16.           andalso (checkRealTimer totalRealTime before fib 25 seq ())
  17.                      < checkRealTimer totalRealTime);
  18.  
  19. local
  20.     val rtmr = startRealTimer ();
  21. in
  22. val test2 = check(checkRealTimer rtmr <= checkRealTimer rtmr
  23.           andalso (checkRealTimer rtmr before fib 25 seq ())
  24.                      < checkRealTimer rtmr);
  25. end
  26.  
  27. local
  28.     val op <= = fn ({usr=usr1, sys=sys1, gc=gc1}, {usr=usr2, sys=sys2, gc=gc2})
  29.     => usr1 <= usr2 andalso sys1 <= sys2 andalso gc1 <= gc1;
  30.     fun cput1 < cput2 = (cput1 <= cput2) andalso (cput1 <> cput2);
  31. in
  32. val test3 = check(checkCPUTimer totalCPUTime <= checkCPUTimer totalCPUTime
  33.           andalso (checkCPUTimer totalCPUTime before fib 25 seq ())
  34.                      < checkCPUTimer totalCPUTime);
  35. val ctmr = startCPUTimer ();
  36. val test4 = check(checkCPUTimer ctmr <= checkCPUTimer ctmr
  37.           andalso (checkCPUTimer ctmr before fib 25 seq ())
  38.                      < checkCPUTimer ctmr);
  39. end;
  40.  
  41. val _ = 
  42. let
  43.     fun time f arg =
  44.     let open Timer
  45.         val cputimer  = startCPUTimer ()
  46.         val realtimer = startRealTimer ()
  47.         val res = f arg
  48.         val {usr, sys, gc} = checkCPUTimer cputimer;
  49.         val rea = checkRealTimer realtimer;
  50.         fun format t = Time.toString t
  51.     in 
  52.         say("User: " ^ format usr ^
  53.         "  System: " ^ format sys ^ 
  54.         "  Gc: " ^ format gc ^ 
  55.         "  Real: " ^ format rea ^ "\n");
  56.         res
  57.     end;
  58.  
  59.     val _ = say "\nEach line below should show roughly \
  60.                  \the same User, System, and Gc times:\n";
  61. in
  62.     map (time fib) [28, 28, 28, 28, 28, 28, 28, 28] seq () 
  63. end 
  64.  
  65. end;
  66.